import uvicorn
from fastapi import FastAPI
from app.core.config import settings
from app.api.v1.api_router import api_router
from app.core.middleware import setup_middleware
from app.core.exception_handlers import setup_exception_handlers
from app.utils.logging import setup_logging
from app.core.lifespan import lifespan

# 设置日志
setup_logging()

app = FastAPI(
    title=settings.PROJECT_NAME,
    version=settings.PROJECT_VERSION,
    openapi_url=f"{settings.API_V1_STR}/openapi.json",
    docs_url=f"{settings.API_V1_STR}/docs",
    redoc_url=f"{settings.API_V1_STR}/redoc",
    lifespan=lifespan
)

# 设置中间件
setup_middleware(app)

# 设置异常处理器
setup_exception_handlers(app)

# 包含API路由
app.include_router(api_router, prefix=settings.API_V1_STR)


@app.get("/")
async def root():
    return {
        "project_name": settings.PROJECT_NAME,
        "version": settings.PROJECT_VERSION,
        "api_version": settings.API_V1_STR
    }

if __name__ == "__main__":
    uvicorn.run(
        "main:app",
        host=settings.SERVER_HOST,
        port=settings.SERVER_PORT,
        reload=settings.DEBUG
    )